/* rqstdxr.p  - Standard Requisition Record ROP                               */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.2 $                                                           */
/*                                                                            */
/* This routine provides the procedures which are executed as a result of the */
/* call from the appropriate Data Type Controllers / structured programs      */
/* It contains business logic pertaining to Standard style Requisitions       */
/* specific to Standard or Global style requisitions.                         */
/*                                                                            */
/* Revision: 1.1     BY:Bill Pedersen           DATE:04/25/00   ECO: *N059*   */
/* $Revision: 1.2 $      BY: Larry Leeth            DATE: 12/01/00  ECO: *N0V1*   */
/*                                                                            */
/*V8:ConvertMode=NoConvert                                                    */

/* ========================================================================== */
/* ******************************* DEFINITIONS ****************************** */
/* ========================================================================== */

/* STANDARD INCLUDE FILES */
{mfdeclre.i}
{pxmaint.i}

/* Define Handles for the programs. */
{pxphdef.i pxgblmgr}
/* End Define Handles for the programs. */


define temp-table ttReq_det like req_det.

/* Some definitions required by gprunp.i can't be declared in an internal     */
/* Procedure, so they're declared here.                                       */
{gprunpdf.i "mcpl" "p"}

/* ========================================================================== */
/* ******************************* MAIN BLOCK ******************************* */
/* ========================================================================== */


/* ========================================================================== */
/* *************************** INTERNAL PROCEDURES ************************** */
/* ========================================================================== */

/*============================================================================*/
PROCEDURE getPurchaseCostForRequisitionItem :

/*------------------------------------------------------------------------------
Purpose:       Obtain the purchase cost for the item when a new PO line is
               created from a standard requisition.
Exceptions:    NONE
Conditions:
        Pre:   None
        Post:  None
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pItemId as character no-undo.
   define input parameter pSiteId as character no-undo.
   define input parameter pItemUMConv as integer no-undo.
   define input parameter pCurrencyId as character no-undo.
   define input parameter pExchangeRate1 as decimal no-undo.
   define input parameter pExchangeRate2 as decimal no-undo.
   define output parameter pPurchaseCost as decimal no-undo.

   define variable glCost as decimal no-undo.
   define variable currentCost as decimal no-undo.
   define variable baseCurr as character no-undo.
   define variable errorCode as integer no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /* GET BASE CURRENCY FROM GLOBAL MANAGER */
      {pxgetph.i pxgblmgr}
      baseCurr = {pxfunct.i &FUNCTION='getCharacterValue' &PROGRAM='pxgblmgr.p'
                            &HANDLE=ph_pxgblmgr
                            &PARAM="input 'base_curr'"}.

      /*TEST IF ITEM MASTER EXISTS*/
      if not can-find(first pt_mstr where pt_part = pItemId) then
         return {&RECORD-NOT-FOUND}.

      /* GET GL & CURRENT COST */
      {gprun.i 'gpsct05.p'
         "(input pItemId,
           input pSiteId,
           input 2,
           output glCost,
           output currentCost)"}

      if pPurchaseCost = 0 then do:
         pPurchaseCost = glCost * pItemUMConv.

         /* CONVERT FROM BASE TO FOREIGN CURRENCY */
         {gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input baseCurr,
              input pCurrencyId,
              input pExchangeRate2,
              input pExchangeRate1,
              input pPurchaseCost,
              input false, /* DO NOT ROUND */
              output pPurchaseCost,
              output errorCode)"}.

      end.
   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.

/*============================================================================*/
PROCEDURE processRead :

/*------------------------------------------------------------------------------
Purpose:       Locate and bring the Standard Requisition Master record into
               scope.
Exceptions:    RECORD-LOCKED, RECORD-NOT-FOUND
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.
   define  parameter buffer req_det for req_det.
   define input parameter pLock as logical no-undo.
   define input parameter pWait as logical no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pLock then do:
         if pWait then do:
            for first req_det
               where req_nbr = pReqNumber
               exclusive-lock:
            end.
         end.
         else do:
            find req_det
               where req_nbr = pReqNumber
               exclusive-lock no-error no-wait.

            if locked req_det then return {&RECORD-LOCKED}.
         end.
      end.
      else do:
         for first req_det where req_nbr = pReqNumber no-lock:
         end.
      end.

      if not available req_det then return {&RECORD-NOT-FOUND}.
   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.

/*============================================================================*/
PROCEDURE processReadReturnTempTable :

/*------------------------------------------------------------------------------
Purpose:       Locate and bring the Standard Requisition Master record into
               scope.
Exceptions:    RECORD-LOCKED, RECORD-NOT-FOUND
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRequisitonId as character no-undo.
   define parameter buffer ttReq_det for ttReq_det.
   define input parameter pLock as logical no-undo.
   define input parameter pWait as logical no-undo.

   define buffer req_det for req_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

         {pxrun.i &PROC='processRead'
            &PARAM="(input pRequisitonId,
                     buffer req_det,
                     input pLock,
                     input pWait)"}

         if return-value <> {&SUCCESS-RESULT} then
            return error return-value.

         buffer-copy req_det to ttReq_det.

   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.


/*============================================================================*/
PROCEDURE validateReqIsApproved :

/*------------------------------------------------------------------------------
Purpose:       Ensure the requisition is approved by Purchasing before allowing
               it to be used on a PO.
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
        Post:  req_det(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if not can-find(first req_det where req_nbr = pReqNumber
         and req_approved = True) then do:
            /*REQUISITION HAS NOT BEEN APPROVED*/
            {pxmsg.i &MSGNUM=195 &ERRORLEVEL={&APP-ERROR-RESULT}}
            return error {&APP-ERROR-RESULT}.
      end.
   end.
   return {&SUCCESS-RESULT}.

END PROCEDURE.

/*============================================================================*/
PROCEDURE validateReqNotOnBlanket :

/*------------------------------------------------------------------------------
Purpose:       Ensure the requisition is not part of a Blanket PO Line
Exceptions:    WARNING-RESULT
Conditions:
        Pre:   None
        Post:  req_det(r), pod_det(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.

   define buffer req_det  for req_det.
   define buffer pod_det  for pod_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='processRead' &PARAM="(input pReqNumber, buffer req_det,
         input no, input no)"}
      if return-value <> {&SUCCESS-RESULT} then
         return return-value.

      for first pod_det fields (pod_nbr pod_line)
         where pod_part = req_part
           and pod_type = "B"
           and pod_req_nbr = pReqNumber
         no-lock:

         /*REQUISITION IS BEING USED ON BLANKET PO:# LN:#.*/
         {pxmsg.i
            &MSGNUM=2955
            &ERRORLEVEL={&WARNING-RESULT}
            &MSGARG1=pod_nbr
            &MSGARG2=pod_line}

         return {&WARNING-RESULT}.
      end.
   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.

/*============================================================================*/
PROCEDURE validateRequisitionSite :
/*------------------------------------------------------------------------------
Purpose:       Ensure the site associated with the requisition is the same as
               the site for the PO
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
        Post:  None
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRequisitionSite as character no-undo.
   define input parameter pPOLineSite as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pRequisitionSite <> pPOLineSite then do:
         /* MESSAGE #2061 - REQUISITION SITE AND PO SITE ARE DIFFERENT */
         {pxmsg.i
            &MSGNUM=2061
            &ERRORLEVEL={&APP-ERROR-RESULT}
            &MSGARG1=pRequisitionSite}
         return error {&APP-ERROR-RESULT}.
      end.
   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.
